package manager;

import remoteEntity.Category;
import remoteEntity.Fileblob;
import remoteEntity.Filedescription;
import remoteEntity.User;
import facade.CategoryFacade;
import facade.FileblobFacade;
import facade.FiledescriptionFacade;
import facade.UserFacade;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
 * FileManager
 *
 * Handles file operations and acts as a proxy between the session facades and
 * the controllers/servlets.
 */
@Stateless
@LocalBean
public class FileManager implements FileManagerRemote {

    @EJB
    private FileblobFacade fileblobFacade;
    @EJB
    private FiledescriptionFacade filedescriptionFacade;
    @EJB
    private CategoryFacade categoryFacade;
    @EJB
    private UserFacade userFacade;

    @Override
    public int addFile(byte[] fileData, String name, String description,
            int category, User user) {
        Fileblob fb = fileblobFacade.insertBlob(fileData);
        return filedescriptionFacade.insertFileDescriptor(name, description,
                user, category, fb);
    }

    @Override
    public byte[] getFileBlob(int filedescriptionId) {
        Filedescription fd = getFile(filedescriptionId);
        if (fd == null) {
            return new byte[0];
        } else {
            return fd.getFileblob().getDataFile();
        }
    }

    //@Override
    public Filedescription getFile(int filedescriptionId) {
        return filedescriptionFacade.find(filedescriptionId);
    }
    
    //@Override
    public List<Filedescription> findAllFileDescriptions(){
        return filedescriptionFacade.findAll();
    }

    //@Override
    public List<Category> getAllCategories() {
        return categoryFacade.findAll();
    }

    @Override
    public void deleteFile(int id) {
        //needs to remove all references in other tables, or change into a 
        // marker for this "inactivated" description (otherwise logs and 
        // other stuff will get messed up
        throw new UnsupportedOperationException("Deleting files not implemented!!");
    }
}
